<?php
namespace app\before\controller;

class WebConfig extends Base
{
    private $tn = 'web_config';

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = [];
            $list  = db($this->tn)->field(true)->where($where)->order('id', 'desc')->page($this->get['page'])->limit($this->get['limit'])->select();
            foreach ($list as $k => $v) {
            }
            $count = db($this->tn)->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        return $this->fetch();
    }

    public function edit($id = 0)
    {
        $model = db('web_config');
        if ($this->post) {

            foreach($this->post as $key=>$val){
                if(in_array($key,['need_min_price','h_yongjin','p_yongjin'])){
                    $val = floatval($val);
                }
                if(in_array($key,['p_zhekou_fabu','p_zhekou_shuliang','h_zhekou_shuliang','h_zhekou_fabu'])){
                    $val = intval($val);
                }
                $model->where('key',$key)->update(['value'=>$val]);

            }
            $this->returnAPI('保存成功',0);
        }
        $data = $model->column('value','key');

        return $this->fetch('',['data'=>$data]);
    }
}
